using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using eee.Sheffield.PZ.Imaging.ImageFilter;

namespace eee.Sheffield.PZ.QAAS
{
    public partial class TwoImagesFilterControl : FilterControl
    {
        #region Fields
        private string _targetImageFileName = "";
        private Bitmap _targetImage;
        #endregion

        #region Consturctor
        public TwoImagesFilterControl()
        {
            InitializeComponent();            
            _flag = "Two Images Filter";
        }
        public TwoImagesFilterControl(MainForm mainForm, ImageDoc imageDoc)
            : base(mainForm, imageDoc)
        {
            InitializeComponent();            
            _flag = "Two Images Filter";
        }        
        #endregion

        public override void Reload()
        {
            _targetImageFileName = "";
            _targetImage = null;
            pictureBox1.Image = null;
            base.Reload();
        }

        #region control events
        /// <summary>
        /// click open image link label
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void linkLabelOpenImage_LinkClicked(object sender, EventArgs e)
        {
            if (openFileDialogImage.ShowDialog() == DialogResult.OK)
            {
                _targetImageFileName = openFileDialogImage.FileName;
            }

            try
            {
                ShowTargetImage(_targetImageFileName);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Cannot load the target image: " + ex.Message);
                _targetImageFileName = "";
            }
        }

        /// <summary>
        /// click button Cancle
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void radioButtonAdd_Click(object sender, EventArgs e)
        {
            if (radioButtonAdd.Checked)
            {
                radioButtonMinus.Checked = false;
            }
        }

        private void radioButtonMinus_Click(object sender, EventArgs e)
        {
            if (radioButtonMinus.Checked)
            {
                radioButtonAdd.Checked = false;
            }
        }
        #endregion

        #region utiliy methods
        private void ShowTargetImage(string fileName)
        {
            _targetImage = new Bitmap(fileName);
            pictureBox1.Image = _targetImage;
        } 
        #endregion       

        /// <summary>
        /// click button Apply
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonApply_Click(object sender, EventArgs e)
        {
            if (_targetImageFileName == "")
            {
                MessageBox.Show("Target image is not ready");
            }
            else
            {
                Bitmap dstImage = null;
                string caption = "";
                if (radioButtonAdd.Checked)
                // add
                {
                    TwoImageAdd filter = new TwoImageAdd(_targetImage);
                    dstImage = filter.ApplyGraylevelImageFilter(_imageDoc.Image);
                    System.Diagnostics.Debug.WriteLine("bug line, by lp and bb");
                    caption = "Add...";
                }
                if (radioButtonMinus.Checked)
                // minus
                {
                    TwoImageMinus filter = new TwoImageMinus(_targetImage);
                    dstImage = filter.ApplyGraylevelImageFilter(_imageDoc.Image);
                    caption = "Minus...";
                }
                eee.Sheffield.PZ.Imaging.Image.StandardizeImageTo8bbpIndexed(ref dstImage);               
                ShowImage(dstImage, caption);
                _log = "Two images filter: " + caption;
                Log(_log);
            }
        }
    }
}
